package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.enums.DelFlag;
import com.ruoyi.system.domain.CaiGouDan;
import com.ruoyi.system.domain.DingDanDetail;
import com.ruoyi.system.domain.HuoPinPinLei;
import com.ruoyi.system.domain.RuKuDan;
import com.ruoyi.system.domain.vo.AddCaiGouDanReq;
import com.ruoyi.system.domain.vo.CaiGouDanQueryReq;
import com.ruoyi.system.domain.vo.CaiGouDanShenHeDetail;
import com.ruoyi.system.mapper.CaiGouDanMapper;
import com.ruoyi.system.service.ICaiGouDanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 采购单信息 服务实现类
 * </p>
 *
 * @author qubaofeng
 * @since 2025-02-18
 */
@Service
public class CaiGouDanServiceImpl extends ServiceImpl<CaiGouDanMapper, CaiGouDan> implements ICaiGouDanService {



    @Override
    public List<CaiGouDan> selectCaiGouDanList(CaiGouDanQueryReq user) {
        return baseMapper.selectCaiGouDanList(user);
    }

    @Override
    public List<CaiGouDan> selectCaiGouDanListNoPage(CaiGouDanQueryReq user) {
        return baseMapper.selectCaiGouDanListNoPage(user);
    }

    @Override
    public List<CaiGouDanShenHeDetail> selectCaigoudanDetail(Long id) {
        CaiGouDan caiGouDan = baseMapper.selectById(id);
        return baseMapper.selectCaigoudanDetail(caiGouDan);
    }

    @Override
    public boolean audit(Long id, Integer status) {
        CaiGouDan caiGouDan = new CaiGouDan();
        caiGouDan.setStatus(status);
        caiGouDan.setId(id);

        return baseMapper.updateById(caiGouDan) > 0;
    }

    @Override
    public List<HuoPinPinLei> selectPinLeiList(String date) {
        return baseMapper.selectPinLeiList(date);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addCaiGouDan(AddCaiGouDanReq req, String userName) {
        List<CaiGouDan> list = baseMapper.caiGouDanTongJi(req);
        Date now = new Date();
        for(CaiGouDan caiGouDan : list) {
            LambdaQueryWrapper<CaiGouDan> query = new LambdaQueryWrapper<>();
            query.eq(CaiGouDan::getHuoPinId, caiGouDan.getHuoPinId());
            query.eq(CaiGouDan::getRiQi, caiGouDan.getRiQi());
            query.eq(CaiGouDan::getShiJian, caiGouDan.getShiJian());

            CaiGouDan have = baseMapper.selectOne(query, false);
            if(have != null){
                have.setUpdateBy(userName);
                have.setUpdateTime(now);
                have.setBuyCount(caiGouDan.getBuyCount());
                baseMapper.updateById(have);
            } else {
                caiGouDan.setCreateBy(userName);
                caiGouDan.setCreateTime(now);
                baseMapper.insert(caiGouDan);
            }

        }
        return true;
    }

    @Override
    public CaiGouDan selectByDingDanDetailId(Long dingDanDetailId) {
        LambdaQueryWrapper<CaiGouDan> query = new LambdaQueryWrapper<>();
        query.eq(CaiGouDan::getDingDanDetailId, dingDanDetailId);
        query.eq(CaiGouDan::getDelFlag, DelFlag.NOMAL.getCode());
        return getOne(query, false);
    }
}
